/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UI_Accesorios.java
 *
 * Created on May 3, 2010, 3:59:19 PM
 */

package UI;

import Excepciones.DinamicaDato;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import Modelo.BaseDato;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import robot_maker.Accesorio;
import robot_maker.Manager;
import robot_maker.ResultSetTableModel;
/**
 *
 * @author CarlosScipione
 */
public class UI_Accesorios extends javax.swing.JFrame {

    /** Creates new form UI_Accesorios */
    public UI_Accesorios() {
        initComponents();
         this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    int rowMyor;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

     private void llenar (JTable tabla, String query) throws SQLException{
            try{
            try {
                BaseDato buscar = new BaseDato();
            } catch (CommunicationsException ex) {
                        Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        UI_Accesorios.class, "CommunicationsException");
            } catch (DinamicaDato ex) {
            }
            tabla.setModel(new ResultSetTableModel(query));
            if (tabla.getRowCount() > 0) {
                tabla.setRowSelectionInterval(0, 0);
            }
        } catch (ClassNotFoundException ex) {
      Logger.getLogger(UI_Clientes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
      Logger.getLogger(UI_Clientes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void refrescar () throws SQLException{
         llenar(acc_table, "select id \"Codigo\", nombre \"Nombre\", " +
                 "caracteristicas \"Descripcion\" from ACCESORIO");

    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        acc_table = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        acc_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        acc_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                acc_tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(acc_table);

        jLabel1.setText("Accesorios Disponbles:");

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("-");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1))
                .add(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(432, 432, 432))
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 386, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton1)
                            .add(jButton2))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        addAccesorio window = new addAccesorio(this, false, -1);
        window = (addAccesorio) Manager.centra(window);
        window.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            llenar(acc_table, "select id \"Codigo\", nombre \"Nombre\", " +
                    "caracteristicas \"Descripcion\" from ACCESORIO");
        } catch (SQLException ex) {
    Logger.getLogger(UI_Accesorios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowOpened

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         Accesorio eliminar = new Accesorio();

        ResultSet lista = null;
        try {
            lista = BaseDato.ejecutarSqlSelect("select ID from ACCESORIO");
        } catch (DinamicaDato ex) {
        }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
                Object[] options = {"Si",
                    "No",
                    "Cancelar"};
        int n = JOptionPane.showOptionDialog(null,
            "Esto borrara toda la informacion del ACCESORIOS incluyendo " +
            "compras de robots",
            "¿Esta seguro?",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);
                eliminar.borrarBD(lista.getInt(1), n);
            } catch (ProcesamientoDato ex) {

            } catch (SQLException ex) {
                        Sistema.loggear(11, "DELETEBOTON",
                        "EN EL SQL DE EJECUCION",
                        UI_Accesorios.class, "SQLException");
            }
        try {
            refrescar();
        } catch (SQLException ex) {
            Sistema.loggear(11, "DELETEBOTON",
                        "EN EL SQL DE EJECUCION",
                        UI_Accesorios.class, "SQLException");
        }
       
    }//GEN-LAST:event_jButton2ActionPerformed

    private void acc_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_acc_tableMouseClicked
        rowMyor = acc_table.getSelectedRow() + 1;
        if(evt.getClickCount() == 2){
              ResultSet lista = null;
            try {
                lista = BaseDato.ejecutarSqlSelect("select id from ACCESORIO");
            } catch (DinamicaDato ex) {
            }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
            } catch (SQLException ex) {
        Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCCION",
                        UI_Accesorios.class, "SQLException");
            }

        addAccesorio window = null;
            try {
                window = new addAccesorio(this, true, lista.getInt(1));
            } catch (SQLException ex) {
        Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCCION",
                        UI_Accesorios.class, "SQLException");
            }
        window = (addAccesorio) Manager.centra(window);
        window.setVisible(true);
        }
    }//GEN-LAST:event_acc_tableMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UI_Accesorios().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable acc_table;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
